package com.kizuki.springcloud.controller;

import com.kizuki.springcloud.pojo.Dept;
import com.kizuki.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @PostMapping("/dept/add")
    public boolean addDept(@RequestBody Dept dept) {
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/list")
    public List<Dept> queryDepts() {
        return deptService.queryDepts();
    }

    @GetMapping("/dept/get/{id}")
    public Dept queryDeptById(@PathVariable("id") int id){
        return deptService.queryDeptById(id);
    }

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/dept/discovery")
    public Object discovery() {
        // 获取微服务列表的清单
        List<String> services = discoveryClient.getServices();
        System.out.println("discovery = " + services);

        // 得到一个具体的微服务信息,通过具体的微服务id，applicaioinName；
        List<ServiceInstance> instances = discoveryClient.getInstances("PROVIDER");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost() + " " + instance.getPort());
        }
        return this.discoveryClient;
    }
}
